package com.server.servlets;

import com.server.dao.LivroDAO;
import com.server.model.Livro;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CadastraLivroServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {
        
      String titulo = request.getParameter("TITULO");
      String autor   = request.getParameter("AUTOR");
      String data   = request.getParameter("DATA");
      
      Livro livro01       = new Livro ();
      LivroDAO livroDAO01 = new LivroDAO ();
      
     int ultimo_id = livroDAO01.selectMaxId();
      
      livro01.setId(ultimo_id + 1);
      livro01.setTitulo(titulo);
      livro01.setAutor(autor);
      livro01.setData_publicacao(data);
      
     int resultado_insert = livroDAO01.create(livro01);
     
     if (resultado_insert > 0) {
     
      response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Sucesso</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1> Livro: " + titulo + " cadastrado com sucesso !" + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }    
     }else {
         
         
      response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>ERRO</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>ERRO, Livro: " + titulo + " não foi cadastrado, tente novamente !" + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }    
         
         
     }     
 
        }   // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
